package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.DataAnalyze;
import com.sysman.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2018年1月15日 下午7:58:38 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/DrawBack")
public class DrawBackController {
	/**
	 * 退款管理列表
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/drawbacklist")
	public ModelAndView drawbacklist()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/DrawBackMan/drawbackList");
		return model;
	}
	/**
	 * 动态加载退款申请list
	 * @param limit
	 * @param offset
	 * @param type
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/drawbackListAjax")
	public @ResponseBody Map<String,Object> drawbackListAjax(Integer limit,Integer offset,Integer type,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		System.out.println("输出接收的参数是："+limit+"//"+offset+"//"+adminloginid+"//"+type);
		String param = "limit="+limit+"&offset="+offset+"&type="+type+"&adminloginid="+adminloginid;//参数串
		System.out.println("输出这个参数串："+param);
		List<Map<String,Object>> jsonlist=new ArrayList<>();
		Map<String, Object> result = new HashMap<String, Object>();  
		Integer total=0;
		String msg="";
		String code="";
		try {
			JSONObject json = HttpRequest.sendPost("listuserordertorefund", param);
			System.out.println("输出接收到的json数据是："+json);
			
			msg = json.get("msg").toString();
			code =json.get("code").toString();
			if("100".equals(code)) {
				try {
					if(json.get("data").toString()!="null") {
						JSONObject jsonobjectdata =json.getJSONObject("data");
						JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
						total = Integer.parseInt(jsonobjectdata.get("total").toString());
						jsonlist = DataAnalyze.DrawBackJsonToList(jsonarrayrows);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					msg = "数据解析出错！";
					code ="-300";
				}
			}
		} catch (Exception e) {
			System.out.println("出错信息是："+e.getMessage());
			msg = "数据传输错误！";
			code ="-200";
		}
		result.put("msg", msg);
		result.put("rows", jsonlist);
		result.put("code", code);
		result.put("total", total);
        
		return result;
	}
	/**
	 * 提交审核
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/examineDrawback")
	public @ResponseBody Map<String,Object> examineDrawback(HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		String userorderid = request.getParameter("userorderid");
		String isagree = request.getParameter("isagree");
		boolean type = Boolean.parseBoolean(isagree);
		String idea = request.getParameter("idea");
		String param = "adminloginid="+adminloginid+"&userorderid="+userorderid+"&type="+type+"&idea="+idea;
		
		System.out.println("参数param是："+param);
		Map<String, Object> result = new HashMap<>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("checkorderrefund",param);
			System.out.println("服务器返回json信息："+json);
	        code = json.get("code").toString();
	        msg = json.get("msg").toString();
		} catch (Exception e) {
			code = "-200";
	        msg = "数据传输错误";
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
}
